package com.hsw.habit.controller;

import com.hsw.habit.frame.annotation.CurrentUser;
import com.hsw.habit.model.JsonData;
import com.hsw.habit.model.NoteItem;
import com.hsw.habit.model.User;
import com.hsw.habit.service.NoteItemService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.logging.Logger;

@RestController
@RequestMapping("/note")
public class NoteController {

    @Resource
    NoteItemService service;

    @RequestMapping("/addNote")
    public JsonData addItem(@CurrentUser User user, NoteItem noteItem) {
        try {
            noteItem.setUserId(user.getId());
            service.addNoteItem(noteItem);
            return JsonData.success();
        } catch (Exception e) {
            Logger logger = Logger.getLogger("note insert");
            logger.warning(e.getMessage());
            return JsonData.error(4001,e.getMessage());
        }
    }

    @RequestMapping("/updateNote")
    public JsonData updateItem(@CurrentUser User user, NoteItem noteItem) {
        try {
            noteItem.setUserId(user.getId());
            service.updateNoteItem(noteItem);
            return JsonData.success();
        } catch (Exception e) {
            return JsonData.error();
        }
    }

    @RequestMapping("/delNote")
    public JsonData delNote(Integer id) {
        try {
            service.delNoteItem(id);
            return JsonData.success();
        } catch (Exception e) {
            return JsonData.error();
        }
    }

    @RequestMapping("/list")
    public JsonData getList(@CurrentUser User user, Integer startRow) {
        try {
            return JsonData.success(service.findNoteItemByUserId(user.getId(), startRow));
        } catch (Exception e) {
            return JsonData.error();
        }
    }

    @RequestMapping("/index")
    public JsonData getListByGroupId(Integer groupId, Integer startRow) {
        try {
            return JsonData.success(service.findNoteItemByGroupId(groupId, startRow));
        } catch (Exception e) {
            return JsonData.error();
        }
    }

    @RequestMapping("/item")
    public JsonData getItem(Integer id) {
        try {
            return JsonData.success(service.findNoteById(id));
        } catch (Exception e) {
            return JsonData.error();
        }
    }
}
